package com.zhiche.lisa.bms.service.pay.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.zhiche.lisa.bms.dao.mapper.pay.PayAffirmMapper;
import com.zhiche.lisa.bms.dao.model.capital.CapitalArrDtl;
import com.zhiche.lisa.bms.dao.model.capital.CapitalArrangement;
import com.zhiche.lisa.bms.dao.model.fee.FeePayable;
import com.zhiche.lisa.bms.dao.model.pay.PayAffirm;
import com.zhiche.lisa.bms.dao.model.pay.PayAffirmApply;
import com.zhiche.lisa.bms.dao.model.pay.PayAffirmDetail;
import com.zhiche.lisa.bms.dao.model.pay.PayApply;
import com.zhiche.lisa.bms.enums.FeePayableBaseEnum;
import com.zhiche.lisa.bms.pojo.vo.pay.PayAffirmApplyVO;
import com.zhiche.lisa.bms.pojo.vo.pay.PayAffirmDetailVO;
import com.zhiche.lisa.bms.pojo.vo.pay.PayAffirmPageVO;
import com.zhiche.lisa.bms.pojo.vo.pay.PayAffirmVO;
import com.zhiche.lisa.bms.service.capital.CapitalArrDtlService;
import com.zhiche.lisa.bms.service.capital.CapitalArrangementService;
import com.zhiche.lisa.bms.service.fee.FeePayableService;
import com.zhiche.lisa.bms.service.pay.PayAffirmApplyService;
import com.zhiche.lisa.bms.service.pay.PayAffirmDetailService;
import com.zhiche.lisa.bms.service.pay.PayAffirmService;
import com.zhiche.lisa.bms.service.pay.PayApplyService;
import com.zhiche.lisa.bms.service.utils.common.identityCodeUtil.CodeUtil;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.utils.Account.AccountUtil;
import com.zhiche.lisa.core.utils.Account.JwtAccountVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 付款确认 服务实现类
 * </p>
 *
 * @author WangShuiJin
 * @since 2018-10-11
 */
@Service
public class PayAffirmServiceImpl extends ServiceImpl<PayAffirmMapper, PayAffirm> implements PayAffirmService {
    @Autowired
    private CodeUtil codeUtil;
    @Autowired
    private PayAffirmDetailService payAffirmDetailService;
    @Autowired
    private PayAffirmApplyService payAffirmApplyService;
    @Autowired
    PayApplyService payApplyService;
    @Autowired
    private CapitalArrDtlService capitalArrDtlService;
    @Autowired
    private CapitalArrangementService capitalArrangementService;

    @Autowired
    private FeePayableService feePayableService;

    /**
     * 新增付款确认
     *
     * @param vo
     * @param checked
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void insertPayAffirm(PayAffirmDetailVO vo, boolean checked) {
        if (ObjectUtils.isEmpty(vo)) throw new BaseException("新增参数为空！");
        JwtAccountVO account = AccountUtil.getAccountInfoFromSecurityContext();
        if (ObjectUtils.isEmpty(account)) throw new BaseException("没有登录信息！");
        vo.setTenantId(Long.valueOf(account.getTenantId()));
        vo.setPayCode(codeUtil.getPayAffirmCode().getNextCode(null));
        vo.setStatus(0);//状态; 0-新增, 1-已审核
        vo.setGmtCreate(new Date());
        this.insert(vo);

        //付款确认明细
        List<PayAffirmDetail> payAffirmDetailList = vo.getPayAffirmDetailList();
        if (!CollectionUtils.isEmpty(payAffirmDetailList)) {
            payAffirmDetailList.forEach(payAffirmDetail -> {
                payAffirmDetail.setHeadId(vo.getId());
                payAffirmDetailService.insertPayAffirmDetail(payAffirmDetail);
            });
        } else {
            if (checked) throw new BaseException("无付款确认明细");
        }

        //付款确认-支付申请
        List<PayAffirmApplyVO> payAffirmApplyVOList = vo.getPayAffirmApplyVOList();
        if (!CollectionUtils.isEmpty(payAffirmApplyVOList)) {
            payAffirmApplyVOList.forEach(payAffirmApplyVO -> {
                //为了方便前台传参进行VO的转换
                PayAffirmApply payAffirmApply = new PayAffirmApply();
                //付款确认主键ID
                payAffirmApply.setHeadId(vo.getId());
                //支付申请单ID
                payAffirmApply.setCapitalDtlId(payAffirmApplyVO.getId());
                payAffirmApply.setSum(payAffirmApplyVO.getSum());
                payAffirmApply.setNotPaidSum(payAffirmApplyVO.getNotPaidSum());
                payAffirmApply.setPaidSum(payAffirmApplyVO.getPaidSum());
                payAffirmApply.setFleetName(payAffirmApplyVO.getFleetName());
                payAffirmApply.setFleetId(payAffirmApplyVO.getFleetId());
                payAffirmApply.setPayCode(payAffirmApplyVO.getPayCode());
                payAffirmApplyService.insertPayAffirmApply(payAffirmApply);
            });
        } else {
            if (checked) throw new BaseException("无绑定支付申请单");
        }
        /**
         * 创建时间：2018-10-11
         * 已废除的功能代码，以备后患，建议保留半年后清除
         *          //修改排款计划状态（0-新增，1-审核，2-预付款）
         *         EntityWrapper ew = new EntityWrapper();
         *         ew.eq("id",vo.getCapitalArrangementID());
         *         CapitalArrangement capitalArrangement = new CapitalArrangement();
         *         capitalArrangement.setState(2);
         *         capitalArrangementService.update(capitalArrangement,ew);
         */

    }

    /**
     * 修改付款确认
     *
     * @param vo
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void updatePayAffirm(PayAffirmDetailVO vo) {
        if (ObjectUtils.isEmpty(vo) || ObjectUtils.isEmpty(vo.getId())) throw new BaseException("修改参数为空！");

        //得到付款确认的ID
        Long payAffirmApplyID = vo.getId();

        //先删除对应关系再新增
        payAffirmDetailService.deletePayAffirmDetailByHeadId(payAffirmApplyID);

        //付款确认-支付申请
        List<PayAffirmApplyVO> payAffirmApplyVOList = vo.getPayAffirmApplyVOList();
        List<PayAffirmApply> payAffirmApplies = new ArrayList<>();
        payAffirmApplyVOList.forEach(payAffirmApplyVO -> {
            //为了方便前台传参进行VO的转换
            PayAffirmApply payAffirmApply = new PayAffirmApply();
            BeanUtils.copyProperties(payAffirmApplyVO, payAffirmApply);
            vo.setPaySum(vo.getPaySum().add(payAffirmApply.getPaidSum()));
            payAffirmApplies.add(payAffirmApply);
//            //付款确认主键ID
//            payAffirmApply.setHeadId(payAffirmApplyID);
//            //支付申请单ID
//            payAffirmApply.setCapitalDtlId(payAffirmApplyVO.getId());
//            payAffirmApplyService.insertPayAffirmApply(payAffirmApply);
        });
        payAffirmApplyService.editPayAffirmApply(payAffirmApplies, payAffirmApplyID);

        //付款确认明细
        List<PayAffirmDetail> payAffirmDetailList = vo.getPayAffirmDetailList();
        payAffirmDetailList.forEach(payAffirmDetail -> {
            payAffirmDetail.setHeadId(payAffirmApplyID);

            payAffirmDetailService.insertPayAffirmDetail(payAffirmDetail);
        });

        //修改付款确认表数据
        this.updateById(vo);
        /**
         * 创建时间：2018-10-11
         * 已废除的功能代码，以备后患，建议保留半年后清除
         *          CapitalArrangementDtl capitalArrangementDtl = new CapitalArrangementDtl();
         *         //排款计划的主键ID
         *         capitalArrangementDtl.setHeadId(vo.getCapitalArrangementID());
         *         //获取排款计划明细表
         *         List<CapitalArrangementDtl> capitalArrangementDtlList = capitalArrangementDtlService.queryCapitalArrangementDtl(capitalArrangementDtl);
         *         if(capitalArrangementDtlList.isEmpty()) throw new BaseException("找不到任何的排款计划！");
         *         //得到排款计划明细表的支付申请ID
         *         List<Long> payIdList = Lists.newArrayList();
         *         capitalArrangementDtlList.forEach(capitalArrangementDtlData ->{
         *             payIdList.add(capitalArrangementDtlData.getPayId());
         *         });
         *         //根据支付申请单id，得到付款确认明细-支付申请单表
         *         List<PayAffirmApply> payAffirmAppliesList = payAffirmApplyService.queryPayAffirmApplyAll(payIdList);
         *         if(payAffirmAppliesList.isEmpty()) throw new BaseException("找不到任何的支付申请单！");
         *         //得到排款计划的主键ID
         *         Long payAffirmApplyID = payAffirmAppliesList.get(0).getHeadId();
         *         //修改付款确认表数据
         *         EntityWrapper ew = new EntityWrapper();
         *         ew.eq("id",payAffirmApplyID);
         *         this.update(vo,ew);
         *
         *         payAffirmAppliesList.forEach(payAffirmApplies ->{
         *             //先删除对应关系再新增
         *             payAffirmApplyService.deletePayAffirmApplyByHeadId(payAffirmApplyID);
         *             payAffirmDetailService.deletePayAffirmDetailByHeadId(payAffirmApplyID);
         *         });
         *
         *         //付款确认-支付申请
         *         List<PayAffirmApplyVO> payAffirmApplyVOList = vo.getPayAffirmApplyVOList();
         *         payAffirmApplyVOList.forEach(payAffirmApplyVO -> {
         *             //为了方便前台传参进行VO的转换
         *             PayAffirmApply payAffirmApply = new PayAffirmApply();
         *             //付款确认主键ID
         *             payAffirmApply.setHeadId(payAffirmApplyID);
         *             //支付申请单ID
         *             payAffirmApply.setPayApplyId(payAffirmApplyVO.getId());
         *             payAffirmApplyService.insertPayAffirmApply(payAffirmApply);
         *         });
         *
         *         //付款确认明细
         *         List<PayAffirmDetail> payAffirmDetailList = vo.getPayAffirmDetailList();
         *         payAffirmDetailList.forEach(payAffirmDetail -> {
         *             payAffirmDetail.setHeadId(payAffirmApplyID);
         *             payAffirmDetailService.insertPayAffirmDetail(payAffirmDetail);
         *         });
         */
    }

    /**
     * 删除付款确认
     *
     * @param ids
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void deletePayAffirm(List<Long> ids) {
        if (ids.isEmpty()) throw new BaseException("删除参数为空！");
        //删除付款确认
        this.deleteBatchIds(ids);
        ids.forEach(id -> {
            //删除付款确认明细-支付申请
            payAffirmApplyService.deletePayAffirmApplyByHeadId(id);
            //删除付款确认明细
            payAffirmDetailService.deletePayAffirmDetailByHeadId(id);
        });
    }

    @Override
    public PayAffirmVO getPayAffirmDetailByID(Long id) {
        PayAffirmVO payAffirmVO = new PayAffirmVO();

        //根据付款确认主键ID，查询付款确认
        PayAffirm payAffirm = this.getPayAffirmByID(id);
        if (ObjectUtils.isEmpty(payAffirm)) return null;
        BeanUtils.copyProperties(payAffirm, payAffirmVO);

        //根据排款计划ID，得到对应的付款明细
        List<PayAffirmDetail> payAffirmDetailByHeadId = payAffirmDetailService.getPayAffirmDetailByHeadId(id);
        payAffirmVO.setPayAffirmDetailList(CollectionUtils.isEmpty(payAffirmDetailByHeadId) ? new ArrayList<>() : payAffirmDetailByHeadId);

        //根据付款确认主键ID，得到【付款确认-支付申请】明细
        List<PayAffirmApply> payAffirmApplyList = payAffirmApplyService.getPayAffirmApplyByHeadId(id);
        if (payAffirmApplyList.isEmpty()) return null;
        //得到【付款确认-支付申请表】的支付申请ID
        List<Long> capitalDtlIdList = Lists.newArrayList();
        Map<Long, PayAffirmApply> payAffirmApplyMap = new HashMap<>();
        payAffirmApplyList.forEach(payAffirmApply -> {
            capitalDtlIdList.add(payAffirmApply.getCapitalDtlId());
            payAffirmApplyMap.put(payAffirmApply.getCapitalDtlId(), payAffirmApply);
        });
        if (payAffirm.getCapitalId() != null) {
            CapitalArrangement capitalArrangement = capitalArrangementService.selectById(payAffirm.getCapitalId());
            payAffirmVO.setCapitalArrangementCreateTime(capitalArrangement.getGmtCreate());
        } else {
            if (!CollectionUtils.isEmpty(payAffirmApplyList)) {
                Long capitalDtlId = payAffirmApplyList.get(0).getCapitalDtlId();
                if (capitalDtlId != null) {
                    PayApply payApply = payApplyService.selectById(capitalDtlId);
                    if (payApply != null) {
                        payAffirmVO.setCapitalArrangementCreateTime(payApply.getApplyDate());
                    }
                }
            }
        }
        if (Integer.valueOf(0).equals(payAffirm.getType())) {
            // 临时找支付申请单
            List<PayApply> payApplyList = payApplyService.selectBatchIds(capitalDtlIdList);
            if (!CollectionUtils.isEmpty(payApplyList)) {
                payApplyList.forEach(payApply -> {
                    PayAffirmApplyVO payAffirmApplyVO = new PayAffirmApplyVO();
                    Long capitalArrangementDtlId = payApply.getId();
                    PayAffirmApply payAffirmApply = payAffirmApplyMap.get(capitalArrangementDtlId);
                    if (payAffirmApply != null) {
                        BeanUtils.copyProperties(payAffirmApply, payAffirmApplyVO);
                    }
                    payAffirmApplyVO.setPayApply(payApply);
                    payAffirmVO.addPayAffirmApplyVOList(payAffirmApplyVO);
                });
            }
        } else {
            //根据支付申请主键id，查询支付申请列表
            List<CapitalArrDtl> capitalArrangementDtls = capitalArrDtlService.selectBatchIds(capitalDtlIdList);
            if (!CollectionUtils.isEmpty(capitalArrangementDtls)) {
                capitalArrangementDtls.forEach(capitalArrangementDtl -> {
                    PayAffirmApplyVO payAffirmApplyVO = new PayAffirmApplyVO();
                    Long capitalArrangementDtlId = capitalArrangementDtl.getId();
                    PayAffirmApply payAffirmApply = payAffirmApplyMap.get(capitalArrangementDtlId);
                    if (payAffirmApply != null) {
                        BeanUtils.copyProperties(payAffirmApply, payAffirmApplyVO);
                    }
                    payAffirmApplyVO.setCapitalArrDtl(capitalArrangementDtl);
                    payAffirmVO.addPayAffirmApplyVOList(payAffirmApplyVO);
                });
            }
        }


        if (CollectionUtils.isEmpty(payAffirmVO.getPayAffirmApplyVOList())) {
            payAffirmVO.setPayAffirmApplyVOList(new ArrayList<>());
        }
        return payAffirmVO;

        /**
         * 创建时间：2018-10-11
         * 已废除的功能代码，以备后患，建议保留半年后清除
         *          //根据付款确认主键ID，得到排款计划详情
         *         CapitalArrangementDtl capitalArrangementDtl = new CapitalArrangementDtl();
         *         capitalArrangementDtl.setHeadId(id);
         *         //获取排款计划明细表
         *         List<CapitalArrangementDtl> capitalArrangementDtlList = capitalArrangementDtlService.queryCapitalArrangementDtl(capitalArrangementDtl);
         *         if(capitalArrangementDtlList.isEmpty()) return null;
         *         //得到排款计划表的支付申请ID
         *         List payIdList = Lists.newArrayList();
         *         capitalArrangementDtlList.forEach(capitalArrangementDtlFor -> {
         *             payIdList.add(capitalArrangementDtlFor.getPayId());
         *         });
         *         //根据排款计划详情表里面的支付申请ID，对比【付款确认-支付申请表】的，支付申请ID
         *         List<PayAffirmApply> payAffirmApplyList = payAffirmApplyService.queryPayAffirmApplyAll(payIdList);
         *         if(payAffirmApplyList.isEmpty()) return null;
         *         //得到【付款确认-支付申请表】的支付申请ID
         *         List payApplyIdList = Lists.newArrayList();
         *         payAffirmApplyList.forEach(payAffirmApply -> {
         *             payApplyIdList.add(payAffirmApply.getPayApplyId());
         *         });
         *         PayAffirmVO payAffirmVO = new PayAffirmVO();
         *         if(payApplyIdList.isEmpty()) return null;
         *         //根据支付申请主键id，查询支付申请列表
         *         payAffirmVO.setPayAffirmApplyVOList(payApplyService.queryPayApplyByIds(payApplyIdList));
         *         //根据排款计划ID，得到对应的付款确认
         *         PayAffirm payAffirm = capitalArrangementDtlService.getPayAffirmByCapitalArrangementID(id);
         *         BeanUtils.copyProperties(payAffirm,payAffirmVO);
         *         //根据排款计划ID，得到对应的付款明细
         *         payAffirmVO.setPayAffirmDetailList(capitalArrangementDtlService.getPayDetailByCapitalArrangementID(id));
         *         return payAffirmVO;
         */
    }

    /**
     * 分页查询
     *
     * @param page
     * @return
     */
    @Override
    public Page<PayAffirmPageVO> queryPayAffirmPage(Page<PayAffirm> page) {
        // EntityWrapper<PayAffirm> ew = this.queryCheck(page.getCondition());
        // //清空page附加条件值
        // page.setCondition(Maps.newHashMap());
        // page.setRecords(baseMapper.selectPage(page, ew));
        Map<String, Object> condition = page.getCondition();
        Object fleetName = condition.get("fleetName");
        if (Objects.nonNull(fleetName) && org.apache.commons.lang3.StringUtils.isNotBlank(fleetName.toString())) {
            condition.put("fleetNameLike", "%" + fleetName.toString() + "%");
        }
        condition.put("orderByCase", " pay_head.gmt_create desc");
        condition.put("limitStart", (page.getCurrent() - 1) * page.getSize());
        condition.put("limitEnd", page.getSize());
        Page<PayAffirmPageVO> voPage = new Page<>();
        BeanUtils.copyProperties(page, voPage);
        List<PayAffirmPageVO> affirmPageVOList = baseMapper.queryPayAffirmPage(condition);
        if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(affirmPageVOList)) {
            affirmPageVOList.forEach(vo -> {
                Set<String> payApplyCodeSet = new HashSet<>();
                Set<String> fleetNameSet = new HashSet<>();
                List<PayAffirmApply> applyDtlList = payAffirmApplyService.getPayAffirmApplyByHeadId(vo.getId());
                if (!CollectionUtils.isEmpty(applyDtlList)) {
                    applyDtlList.forEach(apply -> {
                        if (!StringUtils.isEmpty(apply.getPayCode())) {
                            payApplyCodeSet.add(apply.getPayCode());
                        }
                        if (!StringUtils.isEmpty(apply.getPayCode())) {
                            fleetNameSet.add(apply.getFleetName());
                        }
                    });
                    if (!CollectionUtils.isEmpty(payApplyCodeSet)) {
                        vo.setPayApplyCodeListStr(org.apache.commons.lang3.StringUtils.join(payApplyCodeSet, "; "));
                    }
                    if (!CollectionUtils.isEmpty(fleetNameSet)) {
                        vo.setFleetNameListStr(org.apache.commons.lang3.StringUtils.join(fleetNameSet, "; "));
                    }
                }
            });
        }
        voPage.setRecords(affirmPageVOList);
        voPage.setTotal(baseMapper.countPayAffirm(condition));
        return voPage;
    }

    @Override
    public PayAffirm getPayAffirmByID(Long id) {
        return this.selectById(id);
    }

    /**
     * 付款确认的审核
     *
     * @param ids
     */
    @Override
    public void verifyPayAffirm(List<Long> ids) {
        EntityWrapper ew = new EntityWrapper();
        ew.in("id", ids);

        PayAffirm payAffirm = new PayAffirm();
        payAffirm.setStatus(1);//状态; 0-新增, 1-已审核
        this.update(payAffirm, ew);
        // 2019-05-29 更新应付账款的付款标识
        updateFeePayableByPaymentId(ids, new Date(), false);
    }

    /**
     * 付款的取消审核
     *
     * @param ids
     */
    @Override
    public void cancelVerifyPayAffirm(List<Long> ids) {
        EntityWrapper ew = new EntityWrapper();
        ew.in("id", ids);

        PayAffirm payAffirm = new PayAffirm();
        payAffirm.setStatus(0);//状态; 0-新增, 1-已审核
        this.update(payAffirm, ew);

        updateFeePayableByPaymentId(ids, new Date(), true);
    }

    /**
     * 查询条件
     *
     * @param condition 查询字段
     * @return
     */
    private EntityWrapper<PayAffirm> queryCheck(Map<String, Object> condition) {
        EntityWrapper<PayAffirm> ew = new EntityWrapper<>();
        ew.orderBy("gmt_create desc");
        if (!CollectionUtils.isEmpty(condition)) {
            //遍历条件
            for (Map.Entry<String, Object> entry : condition.entrySet()) {
                if (!Strings.isNullOrEmpty(entry.getKey())) {
                    //付款单号
                    if ("payCode".equals(entry.getKey()) && !StringUtils.isEmpty(entry.getValue())) {
                        ew.eq("pay_code", entry.getValue());
                    }
                    //付款开始日期
                    if ("beginDate".equals(entry.getKey()) && !StringUtils.isEmpty(entry.getValue())) {
                        ew.ge("pay_date", entry.getValue());
                    }
                    //付款结束日期
                    if ("endDate".equals(entry.getKey()) && !StringUtils.isEmpty(entry.getValue())) {
                        ew.le("pay_date", entry.getValue());
                    }
                    //付款类型; 10-发票, 20-现金
                    if ("payType".equals(entry.getKey()) && !StringUtils.isEmpty(entry.getValue())) {
                        ew.eq("pay_type", entry.getValue());
                    }
                    if ("gmtCreateStart".equals(entry.getKey()) && !StringUtils.isEmpty(entry.getValue())) {
                        ew.ge("gmt_create", entry.getValue());
                    }
                    if ("gmtCreateEnd".equals(entry.getKey()) && !StringUtils.isEmpty(entry.getValue())) {
                        ew.le("gmt_create", entry.getValue());
                    }
                }
            }
        }
        return ew;
    }


    /**
     * 付款审核的更新应付账款的付款标识
     *
     * @param paymentIdList
     * @param paymentDate
     * @param isCallBack    是否回退标识
     */
    private void updateFeePayableByPaymentId(List<Long> paymentIdList, Date paymentDate, boolean isCallBack) {
        if (!CollectionUtils.isEmpty(paymentIdList)) {
            List<PayAffirmApply> payAffirmApplyList = payAffirmApplyService.getPayAffirmApplyByHeadIdList(paymentIdList);
            if (CollectionUtils.isEmpty(payAffirmApplyList)) {
                return;
            }

            Set<Long> payIdSet = new HashSet<>();
            List<FeePayable> updateFeePayableList = Lists.newArrayList();
            payAffirmApplyList.stream()
                    .filter(payAffirmApply -> Objects.nonNull(payAffirmApply.getCapitalDtlId()))
                    .forEach(payAffirmApply -> {
                        // 未核销金额
                        BigDecimal notPaidSum = payAffirmApply.getNotPaidSum();
                        // 获取排款明细
                        CapitalArrDtl capitalArrDtl = capitalArrDtlService.selectById(payAffirmApply.getCapitalDtlId());
                        // 判断
                        if (Objects.nonNull(capitalArrDtl) && !payIdSet.contains(capitalArrDtl.getPayId())) {

                            List<Long> payIdList = Lists.newArrayList();
                            payIdList.add(capitalArrDtl.getPayId());
                            List<FeePayable> feePayableList = feePayableService.queryFeePayableListByPayApplyIdList(payIdList);

                            if (!CollectionUtils.isEmpty(feePayableList)) {
                                feePayableList.forEach(feePayable -> {

                                    if (isCallBack) {
                                        // 回退
                                        feePayable.setIsPayment(FeePayableBaseEnum.NO.getCode());
                                        feePayable.setPaymentDate(null);
                                    } else {
                                        // 正常操作
                                        // 判断是否部分还是全部付款
                                        if (Objects.nonNull(notPaidSum)) {
                                            if (notPaidSum.compareTo(BigDecimal.ZERO) > 0) {
                                                // 未核销金额不为0 - 部分核销
                                                feePayable.setIsPayment(FeePayableBaseEnum.PART.getCode());
                                            } else {
                                                // 已全部核销
                                                // feePayable.setIsPayment(FeePayableBaseEnum.ALL.getCode());
                                                // 2019-05-30 修改, 全部付款时候标识和排款标识统一
                                                feePayable.setIsPayment(feePayable.getIsArrPay());
                                            }
                                        }
                                        // 付款确认时间
                                        feePayable.setPaymentDate(paymentDate);
                                    }
                                    feePayable.setGmtModify(new Date());
                                });
                                updateFeePayableList.addAll(feePayableList);
                            }
                            payIdSet.add(capitalArrDtl.getPayId());
                        }
                    });

            // 更新
            if (!CollectionUtils.isEmpty(updateFeePayableList)) {
                feePayableService.updateAllColumnBatchById(updateFeePayableList);
            }
        }
    }
}
